/**
 * @Title: insuredMedicallistAdd_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2015
 * Company:中国平安
 *
 * @Project mhis_web
 * @author EX-LILIUZHU001
 * @date 2015年2月5日 下午2:22:06
 * @version V1.0
 */
;
(function(){
	'use strict';	
	var app = angular.module('fwa.mainApp');
	app.controller('IntegrityMedicalListAddCtrl',['$scope','BaseURL','IntegrityMedicalService','Modal','$stateParams','CommunityService','IntegrityArtificialScoreService','$modal',
	                                             function($scope,baseUrl,IntegrityMedicalService,Modal,$stateParams,CommunityService,IntegrityArtificialScoreService,$modal){
		$scope.form = {};
		var currentPage = 1;
		$scope.form.date = {};
		$scope.integrityMedicalService = IntegrityMedicalService;
		IntegrityMedicalService.initMedicalOrgInfoList();
		var cellTemplate = 
			'<td>'+
				'<a  ng-click="queryScoreDetail(data.taskNo,data.insuranceName,data.DOCTORCODE,data.actNo)">{{data.tranName}}</a>'+
			'</td>',
			indexNo = '<td>'+
				'{{$index+1}}'+
			'</td>';
		
		$scope.columns = [
		      		    {label:'序号',map:'', width:'10%', cellTemplate:indexNo},
		      		    {label:'姓名',map:'insuranceName', width:'25%'},
		      		    {label:'证件号',map:'certificateNo', width:'25%'},
		      		    {label:'违规记分值',map:'pointTotal', width:'15%',cellClass:'text-center'},
		      		    {label:'状态',map:'status', width:'15%',cellClass:'text-center'},
		      		    {label:'操作',map:'doctorCode', width:'10%',cellClass:'text-center', cellTemplate:cellTemplate}
		      		 ];
		var params = function (){
			var doctorCode=null;
			if($scope.form.doctorCode&&!angular.isUndefined($scope.form.doctorCode)){
				doctorCode = $scope.form.doctorCode;
			}
			var orgCode=null;
			if($scope.form.orgCode&&!angular.isUndefined($scope.form.orgCode)){
				orgCode = $scope.form.orgCode;
			}
			var start = 0,
			 	limit = 20;
			if(parseInt(currentPage) > 1){
                  start = (parseInt(currentPage) - 1) * limit;
            }
			var params = {
					startDate:$scope.form.date.start,
					endDate:$scope.form.date.end,
					doctorCode:doctorCode,
					orgCode:orgCode,
					start:start,
					limit:limit
			}
			return params;
		}
		function queryIntegrityMedical() {
			var par = params();
			if(!par.startDate||!par.endDate){
				Modal.alert("案发时间为必录！");
				return null;
			}
			IntegrityMedicalService.getInsuredMedicalListAdd(params()).
			success(function(res){
				$scope.tableData = res.result;
				if(res.totals.total){
				$scope.totalCount = res.totals.total;
				}
			});
		}
		 $scope.onSubmit = function(){
         	currentPage = 1;
         	queryIntegrityMedical();
		}
		
		$scope.$on('pageTool.skipPage',function(e,v){
			var id=e.targetScope.id;
		     if(id=='parentPageTool'){
				currentPage = v;
				queryIntegrityMedical();	 
		     }
		});
		
		$scope.$watch('form.doctorCode', function(v){
			if(v){
				$scope.onSubmit();
			}
		});
		
		var clear = function (){
			$scope.modalName=null;
			IntegrityMedicalService.doctorName = null;
			IntegrityMedicalService.doctorCode = null;
			IntegrityMedicalService.startDate = null;
			IntegrityMedicalService.endDate = null;	
			IntegrityMedicalService.taskNo = null;
		}
		
		//操作弹框
		$scope.queryScoreDetail = function (taskNo,insuranceName,doctorCode,status){			
			IntegrityMedicalService.doctorName = insuranceName;
			IntegrityMedicalService.doctorCode = doctorCode;
			IntegrityMedicalService.startDate = $scope.form.date.start;
			IntegrityMedicalService.endDate = $scope.form.date.end;	
			IntegrityMedicalService.taskNo = taskNo;			
			var views="";
			var controller="";
			if(status=='N2015001'){
				views="integrityArtificialScoreAddDialog_view.html";
				controller="IntegrityArtificialScoreCtrl";
				$scope.modalName="修改";
			}else{
				IntegrityMedicalService.status=status;
				views="integrityArtificialScoreDimissDialog_view.html";
				controller="IntegrityArtificialScoreDetailCtrl";
			}
			var scope=$scope.$new();
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/achievements/integrity/'+views,
			      backdrop:'static',
				  size:'sm',
    			  scope:scope,
				  controller: controller
			    });
			//接收返回数据
			 modalInstance.result.then(function(isTrue) {//true:撤回成功
				 if (isTrue) {
					 $scope.onSubmit();
					}
	 	    });
		}
		
		//新增弹框
		$scope.addArtificialScore = function (){
			clear();
			$scope.modalName="新增";
			var scope=$scope.$new();
			var modalInstance = $modal.open({
				  templateUrl: baseUrl + 'views/hcpms/achievements/integrity/integrityArtificialScoreAddDialog_view.html',			     
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'IntegrityArtificialScoreCtrl'
			    });
			 //接收返回数据
			 modalInstance.result.then(function(isTrue) {//false:取消不操作，true:刷新列表
				 if (isTrue) {
					 $scope.onSubmit();
				}
	 	    });
		}
		/**
		 * 得到要执行批量的数据
		 */
		function getBatchArtificialScoreData(){
			var batchArtificialScoreList=[];
			angular.forEach($scope.tableData,function(data){
				if(data.checked){
					batchArtificialScoreList.push(data);
				}
			});
			return batchArtificialScoreList;
		}
		//批量提交
		$scope.batchArtificialScore=function(){
			var batchArtificialScoreList=getBatchArtificialScoreData();
			$scope.batchArtificialScoreList=null;
			var checkedValidData=true;
			if(batchArtificialScoreList.length>0){
				$scope.batchArtificialScoreList=batchArtificialScoreList;
				for(var i=0;i<batchArtificialScoreList.length;i++){
					if(batchArtificialScoreList[i].actNo==='N2015002'){
						checkedValidData=false;
					}
				}
			}			
			if(!checkedValidData){
				Modal.alert("不能批量提交含有撤销的数据!");
				return;
			}
			if(!$scope.batchArtificialScoreList){
				Modal.alert("请选择要操作的数据!");
			}else{
				Modal.confirm('确认批量提交吗？',function() {					
					IntegrityArtificialScoreService.batchArtificialScore({batchArtificialList:$scope.batchArtificialScoreList}).success(function(res){						
						$scope.onSubmit();	
						Modal.alert(res.msg);
					});
				});
			}
		}
		//重置
		$scope.onReset = function (){
			//默认日期 当前日期的前一个月
			CommunityService.initDate("yyyy-MM-dd").success(function(res) {
				var endDate = res.formatDate;
				var startDate = CommunityService.lastYear(endDate);
				$scope.form.date = {
						start: startDate,
						end: endDate
				};
			});

			$scope.form.doctorCode = '';
			$scope.form.doctorName = '';
			$scope.form.orgCode = '';
			$scope.form.orgName = '';
			IntegrityMedicalService.ownerIntegritys = [];
		}
		
		//弹出医生查询框
		$scope.integrityMedical = function(){
			var scope = $scope.$new();
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/achievements/integrity/integrityMedicalDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'IntegrityMedicalDialogCtrl'
			    });
		}
		//弹出机构查询框
		$scope.integrityOrg = function(){
			var scope = $scope.$new();
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/achievements/integrity/integrityOrgDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'IntegrityOrgDialogCtrl'
			    });
		}
		
		var startDate = $stateParams.startDate;
		var endDate = $stateParams.endDate;
		
		if (!startDate 
				&& !endDate) {
				//默认日期 当前日期的前一个月
				CommunityService.initDate("yyyy-MM-dd").success(function(res) {
					endDate = res.formatDate;
					startDate = CommunityService.lastYear(endDate);
					$scope.form.date = {
							start: startDate,
							end: endDate
					};
					$scope.onSubmit();
				});
			}
			else {
				if (startDate 
					&& !endDate) {
					endDate = CommunityService.backwardYear(startDate);
				} 
				if (!startDate 
					&& endDate) {
					startDate = CommunityService.lastYear(endDate);
				}
				$scope.form.date = {
						start: startDate,
						end: endDate
				};
				$scope.onSubmit();
			}
		
		//导出csv
		$scope.exportIntegrityIllegalToCsv=function (){
			var params = params();
			var url = baseUrl+"/integrityMedicalDesc/exportIntegrityIllegalToCsv.do?"+jQuery.param(params);
			window.location.href = url;
		}
		// 导出
		$scope.onExport = function() {
    		$scope.loading = true;
			var exportTotalCount = 0;//总条数
			//医务人员详情查询列表			
			IntegrityMedicalService.getInsuredMedicalListAdd(params()).
			success(function(res){
				if(res.totals.total){
					exportTotalCount = res.totals.total;//获取总条数
				}				
//			    Modal.alert("总条数:"+exportTotalCount);	
				Modal.alert("导出成功！");
				if(exportTotalCount && exportTotalCount > 30000){
					Modal.alert("数据量超过3万条不能导出，请重新筛选！");
					return;
				}else{
					var par = params();
					var startDate = par.startDate,
					endDate = par.endDate,
					doctorCode = par.doctorCode,
					orgCode = par.orgCode,
					start = par.start,
					actCurrent = par.actCurrent,
					limit = par.limit;
					if(orgCode == null){
						orgCode = '';
					}
					if(doctorCode == null){
						doctorCode = '';
					}
					var url= baseUrl+"integrityMedicalDesc/insuredMedicalListAddExport.do?start="+start+"&limit="+limit+"&startDate="+startDate
					+"&endDate="+endDate+"&doctorCode="+doctorCode+"&orgCode="+orgCode+"&actCurrent="+actCurrent+"&excelName="+"医务人员登记导出";
				   window.location.href = url;
				}
			});
    		$scope.loading = false;
		}
	}]).
	controller('InsuredQueryDialogCtrl',
			['$scope','BaseURL','$stateParams','$modalInstance', 'Modal','IntegrityMedicalService','$state', 
			 function($scope, baseUrl,$stateParams,$modalInstance, Modal,IntegrityMedicalService,$state){
				$scope.currentTab = null;
				$scope.tabs = [  {name:'违规记分情况',currentTab:'0',type:'0'},
					             {name:'预警稽核情况',currentTab:'1',type:'1'},
					             {name:'智能审核情况',currentTab:'2',type:'2'}
				            ];
				$scope.currentTab = $scope.tabs[0];
				$scope.tabOne = true;
				//切换tab页
				$scope.tabClick = function(tab) {
					$scope.currentTab = $scope.tabs[tab.type];
					tabFunction(tab.type);
				}
				var isTrueTab = function(type){
					$scope.tabOne = false;
					$scope.tabTwo = false;
					$scope.tabThree = false;
					if(type == '0'){
						$scope.tabOne = true;
					}else if(type == '1'){
						$scope.tabTwo = true;
					}else if(type == '2'){
						$scope.tabThree = true;
					}
					return true;
				}
				//智能审核情况
				var smartAuditsForThe = function(){
					$scope.billNum = '1000条';
					$scope.billAmount = '14755979.71元';
					$scope.reminderTimes = '5次';
					$scope.numberBeforehand = '3次';
					$scope.numberSomething = '2次';
					$scope.rules = [
					                {"result":"10001","name":"可疑","num":"7845","money":"524"},
					                {"result":"10002","name":"高度可疑","num":"222577","money":"4655"},
					                {"result":"20001","name":"违规","num":"457","money":"89755"}
					                ];
				}
				//tab切换查询
				var tabFunction = function(type){
					if(isTrueTab(type)){
						if(type == '0'){
							//queryIntegrityMedicalDesc(params());//违规记分情况
						}else if(type == '1'){
							//queryIntegrityMedicalDesc(params());//预警稽核情况
						}else if(type == '2'){
							smartAuditsForThe();//智能审核情况
						}
					}
				}
				//导出
//				$scope.onExport = function() {
//					if($scope.tabOne){
//						
//					}else if($scope.tabTwo){
//						
//					}else if($scope.tabThree){
//						
//					}
//				}
				
				var params = function(){
					var startDate = "";
					if(IntegrityMedicalService.startDate&&!angular.isUndefined(IntegrityMedicalService.startDate)){
						startDate = IntegrityMedicalService.startDate;
					}
					var endDate="";
					if(IntegrityMedicalService.endDate&&!angular.isUndefined(IntegrityMedicalService.endDate)){
						endDate = IntegrityMedicalService.endDate;
					}
					var doctorCode="";
					if(IntegrityMedicalService.doctorCode&&!angular.isUndefined(IntegrityMedicalService.doctorCode)){
						doctorCode = IntegrityMedicalService.doctorCode;
					}
					var params = {
							startDate:startDate,
							endDate:endDate,
							doctorCode:doctorCode
					}
					return params;
				}
				
				function queryIntegrityMedicalDesc(params) {
					IntegrityMedicalService.getIntegrityMedicalDesc(params).
					success(function(res){
						$scope.tableData = res.integrityMedicalDesc;
						angular.forEach($scope.tableData,function(data,index){
							data.show = true;
						});
						$scope.happenDate =  IntegrityMedicalService.startDate+'至'+IntegrityMedicalService.endDate;
						$scope.doctorName =  IntegrityMedicalService.doctorName;
					});
				}
				queryIntegrityMedicalDesc(params());
				
				$scope.expanderRow = '<td colspan="12" style="max-height:200px;padding:8px;"> <div style="width:840px;" >'
										+ '<table class="table fwa-table fwa-table-bordered" >'
											+'<thead>'
												+ '<th>记分原因</th>'
												+ '<th>案发日期</th>'
												+ '<th>暂停医保服务权限时间</th>'
												+ '<th>违规记分值</th>'
											+'</thead>'
											+'<tbody>'
											+'<tr ng-repeat="data in tableDataMark">'
											+'<td title="{{data.reason}}"><div ng-bind="data.reason"></div></td>'
											+'<td ><div ng-bind="data.happenDate"></div></td>'
											+'<td ><div ng-bind="data.suspendPayExecutionTime"></div></td>'
											+'<td ><div class="text-center" ng-bind="data.violationsScores"></div></td>'
											+'</tr>'
											+'</tbody>'
										+'</table>'
									  + '</div></td>';
				
				//点击明细事件
				$scope.expandChange = function (data){
					var par = {
							startDate:IntegrityMedicalService.startDate,
							endDate:IntegrityMedicalService.endDate,
							doctorCode:IntegrityMedicalService.doctorCode,
							orgCode:data.healthcareProviderCode
					}
					$scope.queryIntegrityMedicalMark = function (params){
						IntegrityMedicalService.getIntegrityMedicalMark(params).
						success(function(res){
							$scope.tableDataMark = res.integrityMedicalMark;
							angular.forEach($scope.tableData,function(tableData,index){
								if (data.healthcareProviderCode == tableData.healthcareProviderCode) {
									tableData.contentShow = true;
								}
								else {
									tableData.contentShow = false;
								}
							});
						});
					}
					$scope.queryIntegrityMedicalMark(par);
					
				}
				
				//添加一行指令
				var getExpanderTemplate = function(spanType) {
					return '<table-expander-row data-change="expandChange(data)" data-content="data.content" data-content-show="data.contentShow"'+
						'data-span="12" data-span-name="明细" ng-show="data.show" data-template="expanderRow"></table-expander-row>';
				}
				
				$scope.columns = [
					      		    {label:'执业医疗机构名称',map:'healthcareProviderName', width:'22%'},
					      		    {label:'网点编号',map:'healthcareProviderCode', width:'10%'},
					      		    {label:'执行医疗机构地址',map:'fullAddress', width:'20%'},
					      		    {label:'执业时间',map:'qualifiedDate', width:'18%'},
					      		    {label:'违规记分值',map:'violationsScores', width:'10%',cellClass:'text-center'},
					      		    {label:'机构违规记分明细',map:'qualifiedDate', width:'20%',cellClass:'text-center',cellTemplate:getExpanderTemplate()}
//					      		    {label:'备注',map:'remark', width:'16%'}
					      		 ];
				
				function queryWarnInfoCount(params) {
					IntegrityMedicalService.queryWarnInfoCount(params).
					success(function(res){
						$scope.warnCount = res.result;
					});
				}
				queryWarnInfoCount(params());
				//关闭
				$scope.onClose = function(){
    	    		$modalInstance.dismiss();
    	    	}
	}]);
})();